<?php
namespace PsiFramework\Classes\Util\Object;

import("Classes.Model.ModelInterface.IPrimitiveObject");
import("Classes.Util.Database.Query.SqlType");

use \PsiFramework\Classes\Util\Database\Query\SqlType;
use \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The File class represents a file uploaded to the server
 *
 * @package Util.Object
 */
class File implements IPrimitiveObject
{

    protected /*string*/ $_name ;
    protected /*string*/ $_path ;
    protected /*string*/ $_mimeType ;
    protected /*string*/ $_extension ;

    /**
     * Constructor
     *
     * @param     string    $name        the name of the file
     * @param     string    $path        the path where the file is
     * @param     string    $mimeType    the mimetype of the file
     * @return    File    instance
     */
    public function __construct(
        /*string*/ $name=null,
        /*string*/ $path=null,
        /*string*/ $mimeType=null
    )
    {
        $this->_name = $name ;
        $this->_path = $path ;
        $this->_mimeType = $mimeType ;
        
        $this->initExtension();
    }

    /**
     * Getter for the member variable $name
     *
     * @return string    the name of the file
     */
    public function getName()
    {
        return $this->_name ;
    }

    /**
     * Setter for the member variable $name
     *
     * @param string    $name    the name
     */
    public function setName(/*string*/ $name)
    {
        $this->_name = $name ;
    }

    /**
     * Getter for the member variable $path
     *
     * @return string    the path of the file
     */
    public function getPath()
    {
        return $this->_path ;
    }

    /**
     * Setter for the member variable $path
     *
     * @param string    $path    the path to the file
     */
    public function setPath(/*string*/ $path)
    {
        $this->_path = $path ;
    }

    /**
     * Getter for the member variable $mimeType
     *
     * @return string    the MIME Type of the file
     */
    public function getMimeType()
    {
        return $this->_mimeType ;
    }

    /**
     * Setter for the member variable $mimeType
     *
     * @param string    $mimeType    the MIME type of the file
     */
    public function setMimeType(/*string*/ $mimeType)
    {
        $this->_mimeType = $mimeType ;
    }

    /**
     * Getter for the member variable $extension
     *
     * @return string    the extension of the file
     */
    public function getExtension()
    {
        return $this->_extension ;
    }

    /**
     * Setter for the member variable $extension
     *
     * @param string    $extension    the extension
     */
    public function setExtension(/*string*/ $extension)
    {
        $this->_extension = $extension ;
    }

    public function getFileLink()
    {
        return $this->_path ;
    }

    /**
     * Move the file to the new specified path
     *
     * @param string    $newPath    the path to move the file to
     */
    public function moveTo(/*string*/ $newPath)
    {
        $newPath = $newPath.'/'.$this->_name;
        
        move_uploaded_file($this->getFileLink(), $newPath);
        $this->_path = $newPath ;
    }

    private function initExtension(){
        if (!isNullOrEmptyString($this->_name)) {
        	$parts = explode(".", $this->_name);
        	$this->_extension = strtolower($parts[count($parts)-1]);
        }
        return $this->_extension;
    }
    
    public function serialize(/*boolean*/ $asText=true)
    {
        return $this->getFileLink();
    }

    public function unserialize(/*string*/ $text)
    {
        if (!isNullOrEmptyString($text)) {
            $finfo = new \finfo(FILEINFO_MIME_TYPE);
            $this->_path = $text;
            $this->_mimeType = $finfo->file($this->_path);
            $this->_name = basename($this->_path);
            $this->initExtension();
        }
    }

    public function toString()
    {
        return $this->getFileLink();
    }

    public static function getSqlType()
    {
        return SqlType::$varchar ;
    }

}
